package eudjok

class RadioController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [radioInstanceList: Radio.list(params), radioInstanceTotal: Radio.count()]
    }

    def create = {
        def radioInstance = new Radio()
        radioInstance.properties = params
        return [radioInstance: radioInstance]
    }

    def save = {
        def radioInstance = new Radio(params)
		
		radioInstance.dj = session.dj
        radioInstance.dataCriacao = new Date()
        radioInstance.ultimaAtualizacao = new Date()
		
        if (radioInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'radio.label', default: 'Radio'), radioInstance.id])}"
            redirect(action: "show", id: radioInstance.id)
        }
        else {
            render(view: "create", model: [radioInstance: radioInstance])
        }
    }

    def show = {
        def radioInstance = Radio.get(params.id)
        if (!radioInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'radio.label', default: 'Radio'), params.id])}"
            redirect(action: "list")
        }
        else {
            [radioInstance: radioInstance]
        }
    }

    def edit = {
        def radioInstance = Radio.get(params.id)
		
        if (!radioInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'radio.label', default: 'Radio'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [radioInstance: radioInstance]
        }
    }

    def update = {
        def radioInstance = Radio.get(params.id)
        
		
        radioInstance.dataCriacao = new Date()
        radioInstance.ultimaAtualizacao = new Date()
		
		if (radioInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (radioInstance.version > version) {
                    
                    radioInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'radio.label', default: 'Radio')] as Object[], "Another user has updated this Radio while you were editing")
                    render(view: "edit", model: [radioInstance: radioInstance])
                    return
                }
            }
            radioInstance.properties = params
            if (!radioInstance.hasErrors() && radioInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'radio.label', default: 'Radio'), radioInstance.id])}"
                redirect(action: "show", id: radioInstance.id)
            }
            else {
                render(view: "edit", model: [radioInstance: radioInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'radio.label', default: 'Radio'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def radioInstance = Radio.get(params.id)
        if (radioInstance) {
            try {
                radioInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'radio.label', default: 'Radio'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'radio.label', default: 'Radio'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'radio.label', default: 'Radio'), params.id])}"
            redirect(action: "list")
        }
    }
	def top = {
        
        [radioInstanceList: Radio.list(max: 5, sort:'legal', order:'desc')]
    }
}
